from flask import Flask, render_template, session, request, flash, redirect, url_for

app = Flask(__name__)


# 首页：传递基本变量和列表
@app.route("/player")
def player():
    # 渲染templates/index.html，并传递数据
    return render_template("curry.html")  # ** data 表示将字典拆分为键值对参数


@app.route("/index")
def index():
    my_title = "kunkun的主页"
    # 要传递给模板的数据
    data = {
        "username": "练习生蔡徐坤 ",
        "img_url": "https://p0.itc.cn/images01/20230207/952e7bde938345f18617bb4ca5ee2b50.png",
        "hobbies": ["唱跳", "篮球", "rap"],
    }
    return render_template(
        "index.html",
        title=my_title,
        **data,
        # img_url=data["img_url"],
        # username=data["username"],
        # hobbies=data["hobbies"],
    )  # ** data 表示将字典拆分为键值对参数


# 用户页：传递动态参数
@app.route("/order/<order_id>")
def receipt(order_id):
    # 直接传递参数（键值对形式）
    order = {
        "order_id": order_id,
        "order_date": "2025-08-09 15:30:22",
        "branch": "上海新天地店",
        "pickup_number": "A123",
        "is_delivery": True,
        "delivery_address": "上海市黄浦区淡水路231弄5号301室",
        "items": [
            {"name": "多肉葡萄 (标准冰/标准糖)", "price": 29.00},
            {"name": "烤黑糖波波牛乳 (标准冰/少糖)", "price": 25.00},
            {"name": "芋泥波波牛乳 (热/标准糖)", "price": 26.00},
        ],
    }
    total = 0.0
    for i in order["items"]:
        total += i["price"]
    return render_template("heytea-receipt.html", **order, total=total)


#
@app.route("/order")
def order():
    categories = [
        "全部饮品",
        "奶茶系列",
        "果茶系列",
        "芝士系列",
        "咖啡系列",
        "小食甜点",
    ]
    return render_template("heytea-order-main.html", categories=categories)


@app.route("/marco")
def input_style():
    return render_template("macro.html")


@app.route("/marco_called")
def input_style_called():
    return render_template("macro_called.html")


app.secret_key = "Your_secret_key&^52@!"  # 设置secret_key


@app.route("/home")
def home_page():
    username = session.get("username")
    # 判断session是否存储username的数据
    if "username" in session:
        return render_template("home_page.html", username=username)
    return redirect(url_for("login"))  # 重定向到login页面


@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        # 判断用户输入的用户名是否为admin，密码是否为123
        if request.form["username"] != "admin" or request.form["password"] != "123":
            # 发送登录失败的消息
            flash("用户名或密码错误", category="error")
        else:
            session["username"] = request.form["username"]
            session["password"] = request.form["password"]
            # 发送登录成功的消息
            flash("恭喜您，登录成功", category="info")
            # 登录成功，页面重定向到home_page页面
            return redirect(url_for("home_page"))
    return render_template("login.html")


if __name__ == "__main__":
    app.run(debug=True, port=5011)
